package com.crm.business.crm.controller;


import com.crm.business.admin.service.AdminService;
import com.crm.business.crm.service.HpCrmLeadsSetUpService;
import com.crm.common.admin.entity.BO.AdminUserBO;
//import com.crm.common.admin.entity.VO.AdminUserVO;
import com.crm.common.crm.entity.BO.HpCrmSetupLeadsNumBO;
import com.crm.common.crm.entity.VO.HpCrmSetupLeadsNumVO;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.common.ka.core.feign.admin.entity.AdminUserEntityVO;
import com.crm.business.servlet.ApplicationContextHolder;
import com.crm.common.utils.spring.SpringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/hpCrmLeadsSet")
@Api(tags = "线索分配设置模块接口")
//@SysLog(subModel = SubModelType.CRM_SETUP_LEADS,logClass = CrmLeadsLog.class)
public class HpCrmLeadsSetUpController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private HpCrmLeadsSetUpService hpCrmLeadsSetUpService;

    @ApiOperation("通过条件分页查询员工列表")
    @PostMapping("/queryUserList")
    public BasePage<AdminUserEntityVO> queryUserList(@RequestBody AdminUserBO adminUserBO) {
        adminUserBO.setPageType(1);
//        Result<BasePage<AdminUserVO>> basePageResult = adminService.queryUserList(adminUserBO);
        BasePage<AdminUserEntityVO> basePageResult = SpringUtils.getBean(AdminService.class).queryUserList(adminUserBO);
        List<AdminUserEntityVO> list = basePageResult.getList();
        for (AdminUserEntityVO adminUserVO:list){
            HpCrmSetupLeadsNumVO hpCrmSetupLeadsNumVO = hpCrmLeadsSetUpService.selectHpCrmSetupLeadsNumByownerUserId(Math.toIntExact(adminUserVO.getUserId()));
           if (!StringUtils.isEmpty(hpCrmSetupLeadsNumVO)){
               adminUserVO.setNum(hpCrmSetupLeadsNumVO.getNum());
           }else {
               adminUserVO.setNum(0);
           }

        }
        BasePage<AdminUserEntityVO> data = basePageResult;
        data.setList(list);
        return data;
    }
    @PostMapping("/queryById/{ownerUserId}")
    @ApiOperation("根据ID查询")
    public Result<HpCrmSetupLeadsNumVO> queryById(@PathVariable("ownerUserId") @ApiParam(name = "ownerUserId", value = "ownerUserId") Integer ownerUserId) {
        HpCrmSetupLeadsNumVO hpCrmSetupLeadsNumVO = hpCrmLeadsSetUpService.selectHpCrmSetupLeadsNumByownerUserId(ownerUserId);
        return R.ok(hpCrmSetupLeadsNumVO);
    }
    @PostMapping("/addOrupdate")
    @ApiOperation("修改线索分配的数量")
    public Result addOrupdate(@RequestBody HpCrmSetupLeadsNumBO hpCrmSetupLeadsNumBO) {
        HpCrmSetupLeadsNumVO hpCrmSetupLeadsNumVO = hpCrmLeadsSetUpService.selectHpCrmSetupLeadsNumByownerUserId(hpCrmSetupLeadsNumBO.getOwnerUserId());
        if (ObjectUtils.isEmpty(hpCrmSetupLeadsNumVO)){
            int i = hpCrmLeadsSetUpService.insertHpCrmSetupLeadsNum(hpCrmSetupLeadsNumBO);
            return R.ok();
        }else{
            hpCrmSetupLeadsNumBO.setId(hpCrmSetupLeadsNumVO.getId());
            int i = hpCrmLeadsSetUpService.updateHpCrmSetupLeadsNum(hpCrmSetupLeadsNumBO);
            return R.ok();
        }
    }

}
